﻿using FPC.Offer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;
using NLog;
namespace FPC.Offer.UI.GeneralProcessor.Message
{
    /// <summary>
    /// AddMessage 的摘要说明
    /// </summary>
    public class AddMessage : IHttpHandler, IReadOnlySessionState
    {
        FPC.Offer.Service.Message_AddMessageService SendMessage = new Service.Message_AddMessageService();
        FPC.Offer.Service.Message_ReceiveMessageService ReceiveMessage = new Service.Message_ReceiveMessageService();
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var Actioon = context.Request["Actioon"].ToString();//枚举判断进入哪个方法
            var name = context.Request["name"];
            var FromUserName = context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_NAME_REMEMBER].ToString();
            var UserName = context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_NAME_REMEMBER];
            var AmilyID = Convert.ToInt32(context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_UserID_REMEMBER]);
          
            if (Actioon =="show")
            {
             
                if (context.Request["name"] != "" && context.Request["name"] != null)
                {
                    name = context.Request["name"].ToString();
                }
                if (name != "" && name != null)
                {

                    List<FPC.Offer.Model.Message_SendMessage> ss = SendMessage.Select();
                    var ShowLisst = ss.Where(p => p.MessageTitle == name).ToList();
                    ShowLisst = ShowLisst.Where(p => p.SendState.Replace(" ","") != "已删除").ToList();
                    string cc = "";
                        cc = "操作人警号:" + AmilyID.ToString();
                        cc += ",操作人：" + UserName.ToString() + "查看消息记录";
                    logger.Trace(cc);
                    context.Response.Write(JsonConvert.SerializeObject(ShowLisst));
                }

                else
                {
                    var ShowLisst = SendMessage.Select().Where(p => p.SendState.Replace(" ", "") != "已删除").ToList();
                     context.Response.Write(JsonConvert.SerializeObject(ShowLisst));
                }

            }
            if (Actioon == EnumAction.Add.ToString())
            {
                Model.Message_SendMessage MessAdd = new Model.Message_SendMessage();
                Model.Message_ReceiveMessage ReceiveAdd=new Model.Message_ReceiveMessage();
                MessAdd.ToUser = context.Request["ToUserName"];
                MessAdd.MessageTitle = context.Request["Title"];
                MessAdd.MessageContent = context.Request["Content"];
                Guid id = Guid.NewGuid();              
                MessAdd.MessageId = id;
                MessAdd.MessageDate = DateTime.Now;
                MessAdd.FromUser = context.Session[FPC.Offer.Common.SystemKeys.COOKIE_USER_NAME_REMEMBER].ToString();
                MessAdd.SendState = "保存并已发送";              
                Guid ReceiveID=Guid.NewGuid();
                ReceiveAdd.ReceiveID=ReceiveID;
                ReceiveAdd.MessageID=id;
                ReceiveAdd.MessageState="未读";
                int i = SendMessage.Add(MessAdd); 
                int y=ReceiveMessage.Add(ReceiveAdd);
                if (i > 0&& y > 0)
                {
                    string cc = "";
                        cc = "操作人警号:" + AmilyID.ToString();
                        cc += ",操作人：" + UserName.ToString() + "发送消息";
                    logger.Trace(cc);
                    context.Response.Write(1);
                }
                else
                {
                    context.Response.Write(0);       
                }
            }                     
            //收信息批删
            if (Actioon == "dels")
            {
                string ReceiveID = context.Request["ReceiveID"];
                ReceiveID = ReceiveID.Substring(0, ReceiveID.Length - 1);
                if (ReceiveMessage.me_deletes(ReceiveID) > 0)
                {
                    string cc = "";
                        cc = "操作人警号:" + AmilyID.ToString();
                        cc += ",操作人：" + UserName.ToString() + "删除消息";
                    logger.Trace(cc);
                    context.Response.Write(1);
                }
                else
                {
                    context.Response.Write(0);
                }
            }
            
        }

        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}